package com.imooc.distributedemo.resume.leetcode;

import java.util.Stack;

/**
 * solution20  有效的括号
 */
public class Solution20 {
    public static void main(String[] args) {
        String s = "{[]()}";
        s="()[]{}";
        boolean b = isValidBlank(s);
        System.out.println(b);
    }

    private static boolean isValidBlank(String s) {
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if('('==ch||'['==ch||'{'==ch){
                stack.push(ch);
            }else if(')'==ch||']'==ch||'}'==ch){
                char open = stack.pop();
                if(!(open=='('&&ch==')'||
                        open=='{'&&ch=='}'||
                        open=='['&&ch==']')){
                    return false;
                }
            }

        }
        if(stack.isEmpty()){
            return true;
        }
        return false;
    }
}
